<template>
  <div id="app" class="app-container">
    <el-container class="layout-container">
      <!-- 顶部导航 -->
      <el-header class="app-header">
        <div class="header-content">
          <div class="logo-section">
            <el-icon class="logo-icon" :size="24">
              <Monitor />
            </el-icon>
            <h1 class="app-title">EMS网关管理</h1>
          </div>
          
          <div class="header-actions">
            <el-badge :value="alarmCount" :hidden="alarmCount === 0" class="alarm-badge">
              <el-button :icon="Bell" circle />
            </el-badge>
            
            <el-dropdown @command="handleCommand">
              <el-button :icon="Setting" circle />
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="refresh">刷新状态</el-dropdown-item>
                  <el-dropdown-item command="restart" divided>重启系统</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>

      <!-- 导航菜单 -->
      <el-header class="nav-header" height="60px">
        <el-menu
          :default-active="currentRoute"
          mode="horizontal"
          :ellipsis="false"
          @select="handleMenuSelect"
          class="nav-menu"
        >
          <el-menu-item index="/dashboard">
            <el-icon><Monitor /></el-icon>
            <span>系统状态</span>
          </el-menu-item>
          
          <el-menu-item index="/mqtt-config">
            <el-icon><Connection /></el-icon>
            <span>MQTT配置</span>
          </el-menu-item>
          
          <el-menu-item index="/modbus-config">
            <el-icon><Cpu /></el-icon>
            <span>Modbus配置</span>
          </el-menu-item>
          
          <el-menu-item index="/device-config">
            <el-icon><Setting /></el-icon>
            <span>设备配置</span>
          </el-menu-item>
          
          <el-menu-item index="/protocol-config">
            <el-icon><Document /></el-icon>
            <span>协议配置</span>
          </el-menu-item>
          
          <el-menu-item index="/dispatch-logic">
            <el-icon><DataLine /></el-icon>
            <span>调度逻辑</span>
          </el-menu-item>
          
          <el-menu-item index="/system-config">
            <el-icon><Tools /></el-icon>
            <span>系统配置</span>
          </el-menu-item>
          
          <el-menu-item index="/logs">
            <el-icon><List /></el-icon>
            <span>系统日志</span>
          </el-menu-item>
        </el-menu>
      </el-header>

      <!-- 主内容区域 -->
      <el-main class="app-main">
        <router-view v-slot="{ Component, route }">
          <transition name="fade" mode="out-in">
            <component :is="Component" :key="route.path" />
          </transition>
        </router-view>
      </el-main>
    </el-container>

    <!-- 重启确认对话框 -->
    <el-dialog
      v-model="restartDialogVisible"
      title="重启系统"
      width="400px"
      center
    >
      <div class="restart-dialog">
        <el-icon class="warning-icon" :size="48">
          <WarningFilled />
        </el-icon>
        <p>确定要重启EMS系统吗？</p>
        <p class="warning-text">重启过程中设备数据采集将暂停，请确保操作安全。</p>
      </div>
      
      <template #footer>
        <el-button @click="restartDialogVisible = false">取消</el-button>
        <el-button type="danger" @click="confirmRestart">确认重启</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { 
  Monitor, Connection, Setting, Document, Tools, List, 
  Bell, WarningFilled, Cpu, DataLine 
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useSystemStatus } from '@/composables/useSystemStatus'

const router = useRouter()
const route = useRoute()

// 系统状态
const { systemStatus, connectWebSocket, disconnectWebSocket } = useSystemStatus()

// 响应式数据
const alarmCount = ref(0)
const restartDialogVisible = ref(false)

// 计算属性
const currentRoute = computed(() => route.path)

// 方法
const handleMenuSelect = (index) => {
  router.push(index)
}

const handleCommand = (command) => {
  switch (command) {
    case 'refresh':
      refreshSystemStatus()
      break
    case 'restart':
      restartDialogVisible.value = true
      break
  }
}

const refreshSystemStatus = async () => {
  try {
    // 刷新系统状态逻辑
    ElMessage.success('状态刷新成功')
  } catch (error) {
    ElMessage.error('状态刷新失败')
  }
}

const confirmRestart = async () => {
  try {
    // 重启系统API调用
    await restartSystem()
    restartDialogVisible.value = false
    ElMessage.success('系统重启指令已发送')
  } catch (error) {
    ElMessage.error('重启失败：' + error.message)
  }
}

const restartSystem = async () => {
  // TODO: 调用重启API
  return new Promise((resolve) => {
    setTimeout(resolve, 1000)
  })
}

// 生命周期
onMounted(() => {
  connectWebSocket()
  // 监听告警数量变化
  // TODO: 实现告警监听逻辑
})

onUnmounted(() => {
  disconnectWebSocket()
})
</script>

<style scoped>
.app-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
}

.layout-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.app-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-bottom: 1px solid #e6e6e6;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.header-content {
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  color: #fff;
}

.app-title {
  color: #fff;
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-actions .el-button {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
}

.header-actions .el-button:hover {
  background: rgba(255, 255, 255, 0.3);
}

.alarm-badge {
  margin-right: 8px;
}

.nav-header {
  background: #fff;
  border-bottom: 1px solid #e6e6e6;
  padding: 0 20px;
  height: 60px !important;
  line-height: 60px;
}

.nav-menu {
  height: 60px;
  border-bottom: none;
}

.nav-menu .el-menu-item {
  height: 60px;
  line-height: 60px;
  font-weight: 500;
}

.nav-menu .el-menu-item.is-active {
  color: #1890ff;
  border-bottom: 2px solid #1890ff;
}

.app-main {
  flex: 1;
  padding: 20px;
  background: #f5f5f5;
  overflow-y: auto;
  min-height: 0;
}

.restart-dialog {
  text-align: center;
  padding: 20px 0;
}

.warning-icon {
  color: #faad14;
  margin-bottom: 16px;
}

.warning-text {
  color: #999;
  font-size: 14px;
  margin-top: 8px;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 10px;
  }
  
  .app-title {
    font-size: 18px;
  }
  
  .nav-menu .el-menu-item span {
    display: none;
  }
  
  .app-main {
    padding: 10px;
  }
}
</style>
